<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace library;

/**
 * Klasa statyczna do obsługi stringów
 *
 * @author Damian
 */
class StringMethods {

    const POLISH = "ąćęółńśźżĄĆĘÓŁŃŚŹŻ";

    private static $delimeter = "#";

    private function __construct() {
        
    }

    private function __clone() {
        
    }

    private static function normalize($pattern) {
        return self::$delimeter . trim($pattern, self::$delimeter) . self::$delimeter;
    }

    public static function getDelimeter() {
        return self::$delimeter;
    }

    public static function setDelimeter($delimeter) {
        self::$delimeter = $delimeter;
    }

    /**
     * 
     * @param string $string <p>ciąg w którym szukamy danego wyrazenia</p>
     * @param string $pattern <p>wyrażenie regularne lub poszukiwany podciąg</p>
     * @return null
     */
    public static function match($string, $pattern) {
        preg_match_all(self::normalize($pattern), $string, $matches, PREG_PATTERN_ORDER);
        if (!empty($matches[1])) {
            return $matches[1];
        }
        if (!empty($matches[0])) {
            return $matches[0];
        }
        return null;
    }

    public static function split($string, $pattern, $limit = null) {
        $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;
        return preg_split(self::normalize($pattern), $string, $limit, $flags);
    }

    public static function sanitize($string, $mask) {
        if (is_array($mask)) {
            $parts = $mask;
        } else if (is_string($mask)) {
            $parts = str_split($mask);
        } else {
            return $string;
        }
        foreach ($parts as $part) {
            $normalized = self::normalize("\\{$part}");
            $string = preg_replace("{$normalized}m", "\\{$part}", $string);
        }
        return $string;
    }

    public static function unique($string) {
        $unique = "";
        $parts = str_split($unique);
        foreach ($parts as $part) {
            if (strstr($unique, $part)) {
                $unique.=$part;
            }
        }
        return $unique;
    }

    public static function indexOf($string, $substring, $offset = null) {
        $position = strpos($string, $substring, $offset);
        if (!is_int($position)) {
            return -1;
        }
        return $position;
    }

    public static function lastIndexOf($string, $substring, $offset = null) {
        $position = strrpos($string, $substring, $offset);
        if (!is_int($position)) {
            return -1;
        }
        return $position;
    }

    /**
     * <p>szyfrowanie</p>
     * @param string $pure_string
     * @param string $encryption_key  <p>klucz do szyfrowania</p>
     * @return string <p>zwraca zaszyfrowany string</p>
     */
    public static function encrypt($pure_string, $encryption_key = ENCRYPTION_KEY) {
        $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        $encrypted_string = mcrypt_encrypt(MCRYPT_BLOWFISH, $encryption_key, utf8_encode($pure_string), MCRYPT_MODE_ECB, $iv);
        return $encrypted_string;
    }

    /**
     * <p>odszyfrowany</p>
     * @param string $encrypted_string <p>string zaszyfrowany metodą encrypt</p>
     * @param string $encryption_key [optional] <p>klucz do szyfrowania</p>
     * @return string <p>zwraca odszyfrowany string</p>
     */
    public static function decrypt($encrypted_string, $encryption_key = ENCRYPTION_KEY) {
        $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        $decrypted_string = mcrypt_decrypt(MCRYPT_BLOWFISH, $encryption_key, $encrypted_string, MCRYPT_MODE_ECB, $iv);
        return $decrypted_string;
    }
}
